/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.web.controller;

import com.google.common.collect.Lists;
import com.intelligent.ispc.common.dto.JsonPageResponseDto;
import com.intelligent.ispc.common.dto.JsonResponseDto;
import com.intelligent.ispc.common.utils.ApplicationException;
import com.intelligent.ispc.common.utils.ExceptionMessages;
import com.intelligent.ispc.common.web.Servlets;
import com.intelligent.ispc.core.dto.PSAttributeDto;
import com.intelligent.ispc.core.dto.ProjectDto;
import com.intelligent.ispc.core.service.ProjectService;
import com.intelligent.ispc.utils.SystemConfig;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Rovey.Luo on 2016/2/23.
 */

@Controller
@RequestMapping(value = "/psAttribute")
public class PSAttributeController {
    @Autowired
    private ProjectService projectService;

    @Autowired
    private SystemConfig systemConfig;

    private Integer draw = 0;

    public PageRequest setPageObj(ServletRequest request) {
        Integer start = 0;
        if (StringUtils.isNotBlank(request.getParameter("start"))) {
            start = Integer.valueOf(request.getParameter("start"));
        }

        Integer size = systemConfig.getPageSize();
        if (StringUtils.isNotBlank(request.getParameter("length"))) {
            size = Integer.valueOf(request.getParameter("length"));
        }

        if (StringUtils.isNotBlank(request.getParameter("draw"))) {
            draw = Integer.valueOf(request.getParameter("draw"));
        }

        Integer pageNum = 0;
        if ((start / size) > 0) {
            pageNum = start / size;
        }
        return new PageRequest(pageNum, size);
    }

    @ResponseBody
    @RequestMapping(value = "/findPSAttribute")
    public JsonPageResponseDto findPSAttribute(Model model, ServletRequest request) {

        Subject subject = SecurityUtils.getSubject();
        if (!subject.isPermitted("data:view")) {
            return JsonPageResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
        }
        Page<PSAttributeDto> page = null;
        List<PSAttributeDto> psAttributeDtos = null;

        Map<String, Object> searchParam = Servlets.getParametersStartingWith(request, "search_");
        List<PSAttributeDto> psAttributeList = Lists.newArrayList();

        if (searchParam.get("projectId") != null) {
            String projectId = searchParam.get("projectId").toString();
            String sortField = request.getParameter("sortField");
            page = projectService.findPSAttributeByProject(projectId, null);
            psAttributeDtos = page.getContent();

            JsonPageResponseDto json = JsonPageResponseDto.SUCCESS().setBody(psAttributeDtos);
            json.setRecordsTotal(page.getTotalElements());
            json.setRecordsFiltered(page.getTotalElements());
            json.setDraw(draw);
            return json;
        }
        JsonPageResponseDto json = JsonPageResponseDto.SUCCESS().setBody(Lists.newArrayList());
        json.setRecordsTotal(0);
        json.setRecordsFiltered(0);
        json.setDraw(draw);
        return json;
    }


    @ResponseBody
    @RequestMapping(value = "/addAttribute", method = RequestMethod.POST)
    public JsonResponseDto addAttribute(@RequestBody List<PSAttributeDto> psAttributeDtoList) {
        try {
            Subject subject = SecurityUtils.getSubject();
            if (!subject.isPermitted("data:view")) {
                return JsonResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
            }

            PSAttributeDto psAttributeDto = psAttributeDtoList.get(0);
            projectService.addPSAttribute(psAttributeDto);

        } catch (ApplicationException ex) {
            return JsonResponseDto.FAILURE().setMessage(ex.getMessage());
        }
        return JsonResponseDto.SUCCESS();
    }


    @ResponseBody
    @RequestMapping(value = "deleteDataImportPSA/{id}")
    public JsonResponseDto deletePSA(@PathVariable("id") String id, Model model) {


        Subject subject = SecurityUtils.getSubject();
        if (!subject.isPermitted("data:view")) {
            return JsonResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
        }
        try {
            projectService.deletePSAttribute(id);
        } catch (Exception e) {
            return JsonResponseDto.FAILURE().setMessage(e.getMessage());
        }

        return JsonResponseDto.SUCCESS();
    }

    @ResponseBody
    @RequestMapping(value = "addDataImportPSA/{id}", method = RequestMethod.POST)
    public JsonResponseDto addPSA(@RequestBody List<PSAttributeDto> list, @PathVariable("id") String id, Model model) {


        try {
            Subject subject = SecurityUtils.getSubject();
            if (!subject.isPermitted("data:view")) {
                return JsonResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
            }
            PSAttributeDto psAttributeDto = list.get(0);
            psAttributeDto.setProject(projectService.findById(id));
            projectService.addPSAttribute(psAttributeDto);
        } catch (Exception e) {
            return JsonResponseDto.FAILURE().setMessage(e.getMessage());
        }
        return JsonResponseDto.SUCCESS();
    }

    @ResponseBody
    @RequestMapping(value = "updatePSA", method = RequestMethod.POST)
    public JsonResponseDto updatePSA(@RequestBody List<PSAttributeDto> list) {


        Subject subject = SecurityUtils.getSubject();
        if (!subject.isPermitted("data:view")) {
            return JsonResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
        }

        PSAttributeDto psAttributeDto = list.get(0);

        try {
            projectService.updatePSAttribute(psAttributeDto);
        } catch (ApplicationException e) {
            return JsonResponseDto.FAILURE().setMessage(e.getMessage());
        }
        return JsonResponseDto.SUCCESS().setBody(psAttributeDto);
    }


}
